package cn.btks.backstagemanage.unit.controller;

import cn.btks.backstagemanage.menu.pojo.Menu;
import cn.btks.backstagemanage.unit.pojo.Unit;
import cn.btks.backstagemanage.unit.service.UnitService;
import cn.btks.commonserver.result.MenuSourceEnum;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import cn.btks.commonserver.result.SystemLogEnum;
import cn.btks.commonserver.util.JwtUtil;
import cn.btks.commonserver.valid.SystemLog;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

/**
 * @Author lijun
 * @Date 2021/1/13 13:15
 */
@RestController
@RequestMapping("api/v1.0/unit")
@Api(tags = "单位模块")
@Validated
public class UnitController {

    @Autowired
    UnitService unitService;

    @SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @GetMapping("/findAll")
    @ApiOperation("查询所有单位")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findAll() {
        List<Unit> unitList = unitService.findAll();
        return unitList != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), unitList) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @GetMapping("/findById/{id}")
    @ApiOperation("根据ID查询单位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "单位ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "header", name = "token", value = "token密匙", required = true, dataType = "string")
    })
    public ResultState findById(@PathVariable("id")Long id) {
        Unit unit = unitService.findById(id);
        return unit != null ? new ResultState(StateInfo.FIND_OK.getCode(),
                StateInfo.FIND_OK.getMsg(), unit) : new ResultState(StateInfo.FIND_NULL.getCode(),
                StateInfo.FIND_NULL.getMsg());
    }

    @SystemLog(type = SystemLogEnum.QUERY_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @GetMapping("/findPages")
    @ApiOperation("分页查询单位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNumber", value = "当前页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页行数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "unitName", value = "单位名称", required = false, dataType = "string"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState findPagesByName(Integer pageNumber, Integer pageSize, String unitName, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Optional<Claims> optional = JwtUtil.getClaimsFromToken(token, JwtUtil.jwtsSecret);
        if(optional.isPresent()){
            Claims claims = optional.get();
            Object obj = claims.get("unit");
            Unit unit = JSONArray.parseObject(JSON.toJSONString(obj), Unit.class);
            if(unit != null){
                if(unit.getCityCode().equals("65")){
                    return unitService.findPageList(pageNumber, pageSize, unitName);
                }else {
                    return unitService.findPageList1(pageNumber, pageSize, unitName, unit.getCityId());
                }
            }
        }
        return unitService.findPageList(pageNumber, pageSize, unitName);
    }

    @SystemLog(type = SystemLogEnum.SAVE_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @PostMapping("/save")
    @ApiOperation(value = "新增单位信息", notes = "新增单位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "unitName", value = "单位名称", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "unitAddress", value = "单位地址", required = false, dataType = "string"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState save(@RequestBody Unit unit){
        return unitService.save(unit);
    }

    @SystemLog(type = SystemLogEnum.UPDATE_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @PostMapping("/update")
    @ApiOperation(value = "更新单位信息", notes = "更新单位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "单位ID", required = true, dataType = "long"),
            @ApiImplicitParam(paramType = "query", name = "unitName", value = "单位名称", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "unitAddress", value = "单位地址", required = false, dataType = "string"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState update(@RequestBody Unit unit){
        return unitService.update(unit);
    }

    @SystemLog(type = SystemLogEnum.DELETE_LOG,moduleMessage = MenuSourceEnum.MENU_BACKSTAGE)
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除单位信息", notes = "删除单位信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "单位ID", required = true, dataType = "long"),

            @ApiImplicitParam(paramType = "header", name = "token", value = "token", required = true, dataType = "string")
    })
    public ResultState delete(@PathVariable("id") Long id){
        return unitService.delete(id);
    }
}
